package com.lyh.hkcloud.module.m1300093;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.lyh.hkcloud.R;
import com.lyh.hkcloud.activity.HomeActivity;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.common.ActivityUtil;
import com.lyh.hkcloud.module.common.MyDialog;
import com.lyh.hkcloud.module.m1300093.adapter.ListViewAdapter;
import com.lyh.hkcloud.module.m1300093.service.Service;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.LoadingUtils;
import com.lyh.hkcloud.util.base.SnowFlake;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: huangbincheng
 * @program: hkCloud
 * @description: 网罩领用登记
 * @company—date: 利元数据 2023-03-09 16:28
 **/
public class IndexActivity extends BaseActivity implements View.OnClickListener, ListViewAdapter.DeleteCallback {

    private ImageView goBack;
    private Button btSave;
    private Button btResetting;
    private EditText etMesh;
    private Service service;
    private EditText etArea;
    private ListView lvMesh;
    private List<Map<String, Object>> dataMap = new ArrayList<>();
    private ListViewAdapter listViewAdapter;
    private Integer receiverFocusEvent = 0;
    private EditText etReceiver;
    private TextView tvUser;
    private EditText etCount;

    private EditText etAdminNo;
    private TextView tvAdminName;
    private TextView tvDept;

    private EditText etUserNo;
    private TextView tvUserName;



    /**
     * 物料编号
     */
    List<Map<String,Object>> meshCodeList = new ArrayList<>();
    List<String> codeList = new ArrayList<>();

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getSubmitResId() {
        return R.id.bt_save;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_1300093;
    }

    @Override
    protected int getClearResId() {
        return R.id.bt_resetting;
    }

    @Override
    protected void onBeginInit() {
        service = new Service(IndexActivity.this);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView() {
        goBack = findViewById(R.id.go_back);
        btSave = findViewById(R.id.bt_save);
        lvMesh = findViewById(R.id.lv_mesh);
        etMesh = findViewById(R.id.et_mesh);
        btResetting = findViewById(R.id.bt_resetting);
        etArea= findViewById(R.id.et_area);
        etReceiver = findViewById(R.id.et_receiver);
        etAdminNo = findViewById(R.id.et_admin_no);
        etUserNo = findViewById(R.id.et_user_no);
        tvUser = findViewById(R.id.tv_user);
        etCount = findViewById(R.id.et_count);
        tvAdminName = findViewById(R.id.tv_admin_name);
        tvUserName = findViewById(R.id.tv_user_name);
        tvDept = findViewById(R.id.tv_dept);
        //获得焦点，并关闭软盘
        etMesh.setShowSoftInputOnFocus(false);
        goBack.setOnClickListener(this);
        etMesh.setOnClickListener(this);
        etArea.setOnClickListener(this);
        //获得焦点，并关闭软盘
        etArea.setShowSoftInputOnFocus(false);
        etReceiver.setOnClickListener(this);
        etAdminNo.setOnClickListener(this);
        etUserNo.setOnClickListener(this);
        getSupportActionBar().hide();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listViewAdapter = new ListViewAdapter(this, dataMap, IndexActivity.this);
        listViewAdapter.setDeleteCallback(this);
        lvMesh.setAdapter(listViewAdapter);
    }

    //时间分发方法重写
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //如果是点击事件，获取点击的view，并判断是否要收起键盘
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //获取目前得到焦点的view
            View v = getCurrentFocus();
            //判断是否要收起并进行处理
            if (ActivityUtil.isShouldHideKeyboard(v, ev)) {
                ActivityUtil.hideKeyboard(v.getWindowToken(), this, etMesh);
                //ActivityUtil.hideKeyboard(v.getWindowToken(), this, etArea);
            }
        }
        //这个是activity的事件分发，一定要有，不然就不会有任何的点击事件了
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void initEvent() {
        //回车事件，查询物料信息
        etMesh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //查询
                JSONObject jsonObject = new JSONObject();
                try {
                    //有些数据格式是：A007 2022-11-1，需要去掉空格后面的日期
                    String[] mesh=etMesh.getText().toString().replace(BusinessConstants.SCAN_AREA,"").split(" ");
                    String meshInfo=mesh.length>0?mesh[0]:"";
                    if (codeList.indexOf(meshInfo) != -1) {
                        Toast.makeText(IndexActivity.this, "请不要重复扫描", Toast.LENGTH_SHORT).show();
                        etMesh.setText("");
                        return false;
                    }
                    if (codeList.indexOf(mesh) != -1) {
                        Toast.makeText(IndexActivity.this, "请不要重复扫描", Toast.LENGTH_SHORT).show();
                        return false;
                    }else if(StringUtils.isNotEmpty(meshInfo)){
                        jsonObject.put("cardNumber", meshInfo);
                        queryAssets(jsonObject);
                        /*String count=String.valueOf(meshCodeList.size()+1);
                        etCount.setText("总计："+count);*/
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                etMesh.setText("");
                return true;
            }
        });

        //条码 获取焦点事件
        etMesh.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (BusinessConstants.SCAN_MESH_3.equals(String.valueOf(etMesh.getText()))) {
                        etMesh.setText("");
                    }
                    receiverFocusEvent = 1;
                } else {

                    if (etMesh.getText() == null || "".equals(etMesh.getText().toString())) {
                        etMesh.setText(BusinessConstants.SCAN_MESH_3);
                    }
                }

                receiverFocusEvent = 0;
            }
        });

        //区域编码
        etArea.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (BusinessConstants.SCAN_AREA_1.equals(String.valueOf(etArea.getText()))) {
                        etArea.setText("");
                    }
                    receiverFocusEvent = 1;
                } else {
                    if (etArea.getText() == null || "".equals(String.valueOf(etArea.getText()))) {
                        etArea.setText(BusinessConstants.SCAN_AREA_1);
                    }else{
                        JSONObject jsonObject = new JSONObject();

                        try {
                            jsonObject.put("areaCode", etArea.getText());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        queryAreaInformation(jsonObject);
                    }
                    receiverFocusEvent = 0;
                }
            }
        });

        //区域编码回车事件
        etArea.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                etMesh.requestFocus();
                return true;
            }
        });

        //工号回车事件
        etReceiver.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //etArea.requestFocus();
                return true;
            }
        });
        //资管员工号回车事件
        etAdminNo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                etUserNo.requestFocus();
                return true;
            }
        });
        //使用人工号回车事件
        etUserNo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                etArea.requestFocus();
                return true;
            }
        });

        etReceiver.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.v("借用人",etReceiver.getText().toString());
                Log.v("hasFocus",String.valueOf(hasFocus));
                if(hasFocus){
                    if (BusinessConstants.SCAN_PSN_CODE_1.equals(etReceiver.getText().toString())) {
                        etReceiver.setText("");
                    }
                    receiverFocusEvent = 1;
                }else{
                    if ( etReceiver.getText() == null || "".equals(etReceiver.getText().toString())) {
                        etReceiver.setText(BusinessConstants.SCAN_PSN_CODE_1);
                    }else{
                        JSONObject jsonObject = new JSONObject();

                        try {
                            jsonObject.put("psncode", etReceiver.getText());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        queryPersonnelInformation(jsonObject);
                    }
                    receiverFocusEvent = 0;
                }
            }
        });

        etAdminNo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    if (BusinessConstants.SCAN_PSN_CODE_1.equals(etAdminNo.getText().toString())) {
                        etAdminNo.setText("");
                    }
                    receiverFocusEvent = 1;
                }else{
                    if ( etAdminNo.getText() == null || "".equals(etAdminNo.getText().toString())) {
                        etAdminNo.setText(BusinessConstants.SCAN_PSN_CODE_1);
                    }else{
                        JSONObject jsonObject = new JSONObject();

                        try {
                            jsonObject.put("psncode", etAdminNo.getText());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        queryAdminInfo(jsonObject);
                    }
                    receiverFocusEvent = 0;
                }
            }
        });

        etUserNo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    if (BusinessConstants.SCAN_PSN_CODE_1.equals(etUserNo.getText().toString())) {
                        etUserNo.setText("");
                    }
                    receiverFocusEvent = 1;
                }else{
                    if ( etUserNo.getText() == null || "".equals(etUserNo.getText().toString())) {
                        etUserNo.setText(BusinessConstants.SCAN_PSN_CODE_1);
                    }else{
                        JSONObject jsonObject = new JSONObject();

                        try {
                            jsonObject.put("psncode", etUserNo.getText());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        queryUserInfo(jsonObject);
                    }
                    receiverFocusEvent = 0;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        registerReceiver(receiver, Filter());
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(receiver);
    }

    private static IntentFilter Filter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BusinessConstants.ACTION);
        return intentFilter;
    }


    private void clear(View... ignores){
        super.onClear(ignores);
    }

    @Override
    protected void onClear(View... ignores) {
        MyDialog.show(this, "确定全部清除吗?", new MyDialog.OnConfirmListener() {
            @Override
            public void onConfirmClick() {
                //saveposition = Integer.parseInt(v.getTag().toString());
                //Callback.deletePosition();
                clear(ignores);
                dataMap.clear();
                meshCodeList.clear();
                codeList.clear();
                updateAssetList();
                etReceiver.clearFocus();
                etAdminNo.clearFocus();
                etUserNo.clearFocus();
                etArea.clearFocus();
                etMesh.clearFocus();
                Log.v("clear**", "清空**");
            }
        });
    }

    @Override
    protected void onSubmit() {

        if (BusinessConstants.SCAN_PSN_CODE_1.equals(etReceiver.getText().toString()) || StringUtils.isEmpty(String.valueOf(etReceiver.getText()))) {
            Toast.makeText(IndexActivity.this, "扫描人工号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (BusinessConstants.SCAN_PSN_CODE_1.equals(etAdminNo.getText().toString()) || StringUtils.isEmpty(String.valueOf(etAdminNo.getText()))) {
            Toast.makeText(IndexActivity.this, "资管员工号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }if (BusinessConstants.SCAN_PSN_CODE_1.equals(etUserNo.getText().toString()) || StringUtils.isEmpty(String.valueOf(etUserNo.getText()))) {
            Toast.makeText(IndexActivity.this, "使用人工号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("自动带出".equals(tvUser.getText().toString()) || StringUtils.isEmpty(String.valueOf(tvUser.getText()))) {
            Toast.makeText(IndexActivity.this, "扫描人姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("自动带出".equals(tvAdminName.getText().toString()) || StringUtils.isEmpty(String.valueOf(tvAdminName.getText()))) {
            Toast.makeText(IndexActivity.this, "资管员姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }if ("自动带出".equals(tvUserName.getText().toString()) || StringUtils.isEmpty(String.valueOf(tvUserName.getText()))) {
            Toast.makeText(IndexActivity.this, "使用人姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Editable area = etArea.getText();
        if (StringUtils.isEmpty(String.valueOf(area)) || BusinessConstants.SCAN_AREA_1.equals(area.toString())) {
            Toast.makeText(IndexActivity.this, "区域编码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (codeList.size() == 0) {
            Toast.makeText(IndexActivity.this, "请扫描添加物料信息", Toast.LENGTH_SHORT).show();
            return;
        }
        //禁用提交按钮
        btSave.setEnabled(false);

        //组装参数
        HashMap mainIn=new HashMap();
        try {
            SnowFlake worker1 = new SnowFlake(1, 1);
            Long mainId=worker1.nextId();
            mainIn.put("dataId",mainId);
            mainIn.put("deleteSubTableInsts",new ArrayList<>());
            mainIn.put("eventCategory","pc");
            mainIn.put("sysId","1481460132742299648");
            mainIn.put("formId","1684477768580071424");
            //主表
            JSONObject mainInfo=new JSONObject();
            mainInfo.put("tableName","fa_card_storage");
            JSONObject mainData=new JSONObject();
            mainData.put("id",mainId);
            mainData.put("scan_name",tvUser.getText());
            mainData.put("scan_no",etReceiver.getText());
            mainData.put("admin_no",etAdminNo.getText());
            mainData.put("admin_name",tvAdminName.getText());
            mainData.put("user_no",etUserNo.getText());
            mainData.put("user_names",tvUserName.getText());
            mainData.put("location_code",area);
            mainInfo.put("dataInst",mainData);
            mainIn.put("mainTableInst",mainInfo);
            mainIn.put("modifySubTableInsts",new ArrayList<>());
            mainIn.put("offsetData",new JSONObject());
            //子表
            List<HashMap> detailList=new ArrayList<>();
            HashMap detailnInfo=new HashMap();
            detailnInfo.put("tableName","fa_card_storage_detail");
            List<HashMap> detailData=new ArrayList<>();
            //扫描项
            for(int i=0;i<meshCodeList.size();i++){
                HashMap dataInfo=new HashMap();
                dataInfo.put("id",worker1.nextId());
                dataInfo.put("data_id",mainId);
                dataInfo.put("card_number",meshCodeList.get(i).get("cardNumber"));
                dataInfo.put("ma_num",1);
                detailData.add(dataInfo);
            }
            detailnInfo.put("dataInst",detailData);
            detailList.add(detailnInfo);
            mainIn.put("newSubTableInsts",detailList);
            Log.v("mainIn物料信息提交", mainIn.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        showProgressDialog("提示", "提交中...");
        service.post(JsonUtil.toJSONObject(mainIn), HttpUrlComstants.ADD_FORM_DATA, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Toast.makeText(IndexActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                LoadingUtils.getInstance(IndexActivity.this).hide();
//                onClear();
                onClick(goBack);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error", response.optString("message"));
                LoadingUtils.getInstance(IndexActivity.this).hide();
                btSave.setEnabled(true);
            }
        });
        hideProgressDialog();
    }


    /**
     * 扫描 接收者
     *
     * @author: zhongfan
     * @Date 2022/2/15 21:00
     **/
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            etMesh.clearFocus();
            String msg =intent.getStringExtra(BusinessConstants.TAG);
            Log.i(BusinessConstants.TAG, msg);

            JSONObject jsonObject = new JSONObject();

            try {
                jsonObject.put("cardNumber", msg);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            //添加接收人
            if (receiverFocusEvent == 1) {
                etMesh.setText(msg);
                /*查询网罩信息*/
                //queryMeshInformation(jsonObject);
                return;
            }

            if (codeList.indexOf(msg) != -1) {
                Toast.makeText(IndexActivity.this, "请不要重复扫描", Toast.LENGTH_SHORT).show();
                return;
            }

            showProgressDialog("提示", "查询中...");
            /*查询网罩*/
            //queryMeshInformation(jsonObject);

            hideProgressDialog();
        }
    };


    /**
     * 查询物料信息
     */
    private void queryAssets(JSONObject jsonObject) {
        if(StringUtils.isEmpty(etArea.getText().toString())||BusinessConstants.SCAN_AREA_1.equals(String.valueOf(etArea.getText()))) {
            Toast.makeText(IndexActivity.this, "区域码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        service.post(jsonObject, HttpUrlComstants.QUERY_TEMP_FACARD, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    if (data.length() == 0) {
                        Toast.makeText(IndexActivity.this, "该条码不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONObject meshInfo = data.getJSONObject(0);
                    if (null == meshInfo) {
                        Toast.makeText(IndexActivity.this, "该条码不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONArray meshArray = meshInfo.getJSONArray("result");
                    if (meshArray.length() == 0) {
                        Toast.makeText(IndexActivity.this, "该条码不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject3 = meshArray.getJSONObject(0);
                    if (jsonObject3 == null) {
                        Toast.makeText(IndexActivity.this, "该条码不存在", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    Map<String, Object> map = new HashMap<>();
                    map.put("cardNumber", jsonObject3.optString("card_number") == null ? "" : jsonObject3.optString("card_number"));
                    map.put("maNumber", jsonObject3.optString("ma_number") == null ? "" : jsonObject3.optString("ma_number"));
                    map.put("maName", jsonObject3.optString("ma_name") == null ? "" : jsonObject3.optString("ma_name"));
                    map.put("maMateria", jsonObject3.optString("ma_materia") == null ? "" : jsonObject3.optString("ma_materia"));
                    map.put("maDesc", jsonObject3.optString("ma_desc") == null ? "" : jsonObject3.optString("ma_desc"));
                    String statusName = jsonObject3.optString("ma_status");
                    if ("已报废".equals(statusName)) {
                        Toast.makeText(IndexActivity.this, "该条码已报废", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (codeList.indexOf(map.get("cardNumber")) == -1) {
                        //dataMap.add(map);
                    } else {
                        Toast.makeText(IndexActivity.this, "请不要重复扫描", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    /*meshCodeList.add(map);
                    codeList.add(jsonObject3.optString("card_number"));*/
                    //查询入库扫描记录
                    jsonObject.put("maNumber", map.get("maNumber"));
                    jsonObject.put("areaCode", etArea.getText());
                    queryScanRecord(jsonObject,map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error", response.optString("message"));
                Toast.makeText(IndexActivity.this, "查询物料信息失败", Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**查询入库扫描记录*/
    private void queryScanRecord(JSONObject jsonObject,Map<String, Object> map) {
        service.post(jsonObject, HttpUrlComstants.QUERY_SCAN_RECORD, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    if (null == data) {
                        Log.e("校验通过", response.optString("message"));
                       /* addData(jsonObject, map);
                        return;*/
                    }
                    JSONObject meshInfo = data.getJSONObject(0);
                    if (null == meshInfo) {
                        Log.e("校验通过", response.optString("message"));
                       /* addData(jsonObject, map);
                        return;*/
                    }
                    JSONArray meshArray = meshInfo.getJSONArray("result");
                    if (meshArray.length() == 0) {
                        Log.e("校验通过", response.optString("message"));
                        addData(jsonObject, map);
                        /*return;*/
                    }
                    else  {
                        Toast.makeText(IndexActivity.this, "此条码已入库", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error", response.optString("message"));
                Toast.makeText(IndexActivity.this, "查询物料信息失败" + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
    /**整理数据*/
    private void addData(JSONObject jsonObject3,Map<String, Object> map){
        //先校验成功，再处理数据
        if (codeList.indexOf(map.get("cardNumber")) == -1) {
            dataMap.add(map);
        } else {
            Toast.makeText(IndexActivity.this, "请不要重复扫描", Toast.LENGTH_SHORT).show();
            return;
        }
        meshCodeList.add(map);
        codeList.add(map.get("cardNumber").toString());
        String count=String.valueOf(meshCodeList.size());
        etCount.setText("总计："+count);
        //通知ListView更改数据源
        updateAssetList();
    }

    private void updateAssetList() {
        //通知ListView更改数据源
        if (listViewAdapter != null) {
            listViewAdapter.notifyDataSetChanged();
        } else {
            listViewAdapter = new ListViewAdapter(IndexActivity.this, dataMap, IndexActivity.this);
            lvMesh.setAdapter(listViewAdapter);
        }
        lvMesh.setSelection(dataMap.size() - 1);//设置显示列表的最后一项
    }

    /**
     * 通过网罩编号，查询网罩信息
     *
     * @param
     * @return void
     * @author: 黄斌成
     * @Date 2022/11/1 17:47
     **/
    /*public void queryMeshInformation(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_MESH, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    if(data.length()==0){
                        Toast.makeText(IndexActivity.this, "查询网罩信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONArray meshInfo =data.getJSONArray(0);
                    if (meshInfo.length() == 0) {
                        Toast.makeText(IndexActivity.this, "查询网罩信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    jsonObject3 =meshInfo.getJSONObject(0);
                    Log.v("jsonObj333333:", jsonObject3.toString());

                    //使用人代码
                    meshMap.put("mesCoverCode", jsonObject3.get("mes_cover_code") == null ? "" : jsonObject3.optString("mes_cover_code"));
                    //使用人名称
                    meshMap.put("maName", jsonObject3.get("ma_name") == null ? "" : jsonObject3.optString("ma_name"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error", response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }*/


    @Override
    public void deletePosition(int saveposition) {
        Log.v("saveposition:", String.valueOf(saveposition));
        dataMap.remove(saveposition);
        meshCodeList.remove(saveposition);
        codeList.remove(saveposition);
        listViewAdapter.setData(dataMap);
        listViewAdapter.notifyDataSetInvalidated();
    }

    private ProgressDialog progressDialog;

    /**
     * 提示加载
     */
    public void showProgressDialog(String title, String message) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog.show(IndexActivity.this, title, message, true, false);
        } else if (progressDialog.isShowing()) {
            progressDialog.setTitle(title);
            progressDialog.setMessage(message);
        }

        progressDialog.show();

    }

    /**
     * 隐藏提示加载
     */
    public void hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_back:
                Intent intent = new Intent(IndexActivity.this, HomeActivity.class);
                IndexActivity.this.startActivity(intent);
                break;
            case R.id.et_mesh:
                break;
        }
    }

    /**
     * 通过人员id，查询使用人员信息
     *
     * @param jsonObject
     * @return void
     * @author: huangbincheng
     * @Date 2023/3/9 17:47
     **/
    public void queryPersonnelInformation(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_PERSONNEL_DETAILS, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    JSONObject jsonObject2 = new JSONObject(data.get(0).toString());
                    JSONArray facardinfo = jsonObject2.optJSONArray("psninfo");
                    jsonObject3 = new JSONObject(facardinfo.get(0).toString());
                    Log.v("通过人员id，查询人员信息返回参数:", jsonObject3.toString());

                    if (jsonObject3 == null) {
                        Toast.makeText(IndexActivity.this, "获取领用人信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Map<String, Object> assetsMap = new HashMap<>();
                    //使用人id
                    assetsMap.put("psnid",jsonObject3.getString("psnid"));
                    //使用人代码
                    assetsMap.put("psncode",jsonObject3.getString("psncode"));
                    //使用人名称
                    assetsMap.put("psnname",jsonObject3.getString("psnname"));
                    tvUser.setText(jsonObject3.getString("psnname"));
                    //etCount.setText(jsonObject3.getString("psnname"));
                    //tvDept.setText(jsonObject3.getString("deptname"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }



    /**
     * 通过人员id，查询使用人员信息
     *
     * @param jsonObject
     * @return void
     * @author: huangbincheng
     * @Date 2023/7/26 17:47
     **/
    public void queryAdminInfo(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_PERSONNEL_DETAILS, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    JSONObject jsonObject2 = new JSONObject(data.get(0).toString());
                    JSONArray facardinfo = jsonObject2.optJSONArray("psninfo");
                    jsonObject3 = new JSONObject(facardinfo.get(0).toString());
                    Log.v("通过人员id，查询资管员信息返回参数:", jsonObject3.toString());

                    if (jsonObject3 == null) {
                        Toast.makeText(IndexActivity.this, "获取资管人信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Map<String, Object> assetsMap = new HashMap<>();
                    //使用人id
                    assetsMap.put("psnid",jsonObject3.getString("psnid"));
                    //使用人代码
                    assetsMap.put("psncode",jsonObject3.getString("psncode"));
                    //使用人名称
                    assetsMap.put("psnname",jsonObject3.getString("psnname"));
                    tvAdminName.setText(jsonObject3.getString("psnname"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    /**查询使用人信息*/
    public void queryUserInfo(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_PERSONNEL_DETAILS, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                JSONObject jsonObject3 = null;
                try {
                    JSONObject jsonObject2 = new JSONObject(data.get(0).toString());
                    JSONArray facardinfo = jsonObject2.optJSONArray("psninfo");
                    jsonObject3 = new JSONObject(facardinfo.get(0).toString());
                    Log.v("通过人员id，查询使用人信息返回参数:", jsonObject3.toString());

                    if (jsonObject3 == null) {
                        Toast.makeText(IndexActivity.this, "获取使用人信息为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Map<String, Object> assetsMap = new HashMap<>();
                    //使用人id
                    assetsMap.put("psnid",jsonObject3.getString("psnid"));
                    //使用人代码
                    assetsMap.put("psncode",jsonObject3.getString("psncode"));
                    //使用人名称
                    assetsMap.put("psnname",jsonObject3.getString("psnname"));
                    tvUserName.setText(jsonObject3.getString("psnname"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }


    public void queryAreaInformation(JSONObject jsonObject) {
        service.post(jsonObject, HttpUrlComstants.QUERY_AREA_INFO, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                JSONArray data = response.optJSONArray("data");
                try {
                    Log.v("查询区域信息返回参数:", data.toString());
                    JSONObject areaInfo = data.getJSONObject(0);
                    if (null == areaInfo) {
                        Toast.makeText(IndexActivity.this, "区域码不存在", Toast.LENGTH_SHORT).show();
                        etArea.setText("");
                        return;
                    }
                    JSONArray meshArray = areaInfo.getJSONArray("result");
                    if (meshArray.length() == 0) {
                        Toast.makeText(IndexActivity.this, "区域码不存在", Toast.LENGTH_SHORT).show();
                        etArea.setText("");
                        return;
                    }
                   /* Map<String, Object> assetsMap = new HashMap<>();
                    //使用人id
                    assetsMap.put("psnid",jsonObject3.getString("psnid"));
                    //使用人代码
                    assetsMap.put("psncode",jsonObject3.getString("psncode"));
                    //使用人名称
                    assetsMap.put("psnname",jsonObject3.getString("psnname"));*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

}

